<template>
  <div class="dashboard-container">
    <h2 class="head-big-title">Pesanan Pembelian</h2>
    <Pathnavigation style="margin-left: 3%;"></Pathnavigation>
    <el-button
      round
      v-show="role != dealerMValue"
      type="warning"
      size="medium"
      icon="el-icon-plus"
      style="float: left;margin-left: 3%;"
      @click="$router.push({ path: '/Orders/New' })">Cipta PO</el-button>
    <el-button
      round
      v-show="role != dealerMValue"
      type="info"
      size="medium"
      @click="deleteArray">Padam</el-button>
    <el-form label-position="top" label-width="80px" :model="searchData" style="width: 92%;margin: 20px 4% 0 4%;padding: 10px 2% 3px 2%;border-radius: 10px;background-color: white;text-align: left;clear: both;">
      <el-form-item label="Dealer Utama / Dealer" v-show="role == adminValue" class="one-form-item">
        <el-select v-model="searchData.sellers" @change="getResetLoadData">
          <el-option label="Semua" value=""></el-option>
          <el-option v-for="(item,index) in dealerList" :key="index" :label="item.dealerName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="Nombor Pesanan">
        <el-input v-model="searchData.invoiceId" @blur="getResetLoadData" placeholder="Nombor Pesanan" type="number"></el-input>
      </el-form-item>
      <el-form-item label="Nama Pelanggan">
        <el-input v-model="searchData.dealerName" @blur="getResetLoadData" placeholder="Nama Pelanggan"></el-input>
      </el-form-item>
      <el-form-item label="Status" class="one-form-item">
        <el-select v-model="searchData.state" @change="getResetLoadData" placeholder="Status">
          <el-option label="Semua" value=""></el-option>
          <el-option v-for="(item, index) in $orderStateList" :key="index" :label="item.label" :value="item.value" v-show="!(item.value == -2)"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div style="width: 96%;margin: 20px 2% 0 2%;padding-left: 10px;border-radius: 20px;background-color: rgb(245,246,247);text-align: left;">
      <div v-for="(item,index) in tableData" :key="index" style="width: 100%;border-radius: 10px;background-color: white;padding: 6px 5px 15px 6px;margin-bottom: 10px;">
        <el-checkbox v-model="item.choosed" :true-label="1" :false-label="0" v-if="!(role == dealerMValue)" style="float: left;font-size: 20px;"></el-checkbox>
        <strong style="line-height: 24px;display: inline;margin-left: 6px;">Nombor Pesanan #{{item.oid}}</strong>
        <svg-icon icon-class="ico_view" style="font-size: 24px;float: right;margin-right: 5%;position: relative;bottom: 3px;" @click="handleEdit(item)"></svg-icon>
        <el-divider></el-divider>
        <div style="width: 100%;margin-top: 10px;">
          <strong>{{item.buyerName}}</strong>
          <p>{{item.buyerPhoneNumber}}</p>
        </div>
        <div style="width: 100%;margin-top: 10px;">
          <div style="width: 50%;display: inline-block;">
            <strong>Tarikh</strong>
            <p>{{$getTime(item.createdAt)}}</p>
          </div>
          <div style="width: 49%;display: inline-block;">
            <strong>Tarikh Penghantaran</strong>
            <p>{{ item.deliveryAt ? $getTime(item.deliveryAt) : '***' }}</p>
          </div>
        </div>
        <div style="width: 100%;margin-top: 10px;">
          <div style="width: 60%;display: inline-block;">
            <strong>Status</strong>
            <p v-for="(list, index) in $orderStateList" :key="index" v-show="item.state == list.value">{{list.label}}</p>
          </div>
          <div style="width: 40%;display: inline-block;">
            <strong>Total</strong>
            <p>{{$formatMoneyStr(item.subFee)}}</p>
          </div>
        </div>
      </div>
    </div>
    <el-pagination
      style="float: right;margin: 16px 4% 10px"
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :page-size="searchData.pageNum"
      :current-page="searchData.pageIndex"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { order_queryApi, query_masterDealerApi, delOrderApi } from '@/api/user'

export default {
  name: 'Customers',
  computed: {
    ...mapGetters([
      'name',
      'role'
    ])
  },
  data() {
    return {
      tableData: [],
      dealerList: [],
      searchData: {
        pageNum: 10,
        pageIndex: 1
      },
      willDel: [],
      total: 0,
      orderChart: ''
    }
  },
  created() {
    this.getDealerList()
    this.loadData()
  },
  mounted() {
    this.$removeDetail()
  },
  methods: {
    getResetLoadData() {
      this.searchData.pageIndex = 1
      this.loadData()
    },
    getDealerList() {
      if (this.role === this.adminValue) {
        query_masterDealerApi({ pageNum: 100, pageIndex: 1 }).then(res => {
          this.dealerList = res.data
        }).catch(err => {
          this.dealerList = []
          console.log(err)
        })
      }
      this.dealerList = []
    },
    showDetail(row) {
      this.$router.push('/Orders/Edit/' + row.oid)
    },
    loadData() {
      // this.$filterEmptyArray(this.searchData)
      order_queryApi(this.searchData).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      }).catch(err => {
        console.log(err)
      })
    }, // 得到数据表格
    handleEdit(row) {
      this.$router.push('/Orders/Edit/' + row.oid)
    },
    deleteArray() {
      this.handleSelectionChange()
      this.$confirm('Delete all data', 'Tips', {
        confirmButtonText: 'Yes',
        center: true,
        cancelButtonText: 'No',
        type: 'warning'
      }).then(() => {
        delOrderApi(this.willDel).then(res => {
          this.$message({
            message: 'Berjaya',
            type: 'success'
          })
          this.loadData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: 'Cancel'
        })
      })
    },
    handleSelectionChange() {
      this.willDel = []
      const val = this.tableData
      if (val) {
        if (val.length) {
          if (val.length > 0) {
            let newArray = {}
            for (let i = 0; i < val.length; i++) {
              newArray = val[i].oid
              if (val[i].choosed == 1) {
                this.willDel.push(newArray)
              }
            }
          }
        }
      }
    },
    handleCurrentChange(val) {
      this.searchData.pageIndex = val
      this.loadData()
    }
  }
}
</script>

<style lang="scss" scoped>
  .head-big-title{
    text-align: left;
    font-size: 20px;
    margin: 20px 0 0 3%;
  }
  .dashboard-container{
    margin-top: 20px;
    text-align: center;
  }
  .one-form-item{
    display: block;
    .el-select{
      width: 100%;
    }
    .el-input{
      width: 100%;
    }
  }
  .two-form-item{
    display: inline-block;
    width: 44%;
    .el-select{
      width: 100%;
    }
    .el-input{
      width: 100%;
    }
  }
  .two-form-item +.two-form-item{
    margin-left: 4%;
  }
</style>
<style scoped>
  /deep/.head-search label{
    line-height: 30px;
  }
  /deep/.el-divider{
    margin: 8px 0 10px 0;
  }
</style>
